package com.initialize.appapi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.initialize.appapi.controller.model.AcceptArticleModel;
import com.initialize.pub.mybatis.plus.entity.Article;
import com.initialize.pub.mybatis.plus.service.impl.ArticleServiceImpl;
import com.initialize.pub.token.UserLoginToken;
import com.initialize.pub.util.ToolUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: jane
 * Date: 2020/3/13
 * Time: 9:44 下午
 * Description:
 */
@Api(tags = "文章API")
@RestController
@CrossOrigin
@RequestMapping(value = "/article")
public class ArticleController {

    @Autowired
    ArticleServiceImpl articleService;


    @ApiOperation("获取文章的数量")
    @PostMapping("/count")
    @UserLoginToken
    public R<Integer> getTheNumberOfArticles() {
        return R.ok(articleService.count());
    }


    @ApiOperation("获取文章的列表")
    @GetMapping("/list/{size}")
    @UserLoginToken
    public R<Page<Article>> getListOfArticles(@PathVariable int size) {
        Page<Article> page = articleService.page(new Page<>(size, 10, true));
        Collection<Article> records = page.getRecords();
        records.forEach(o -> {
            String content = o.getContent();
            String s = ToolUtil.removeMarkdown(content);
            if (s.length() > 50) {
                s = s.substring(0, 49) + "...";
            }
            o.setContent(s);
        });
        return R.ok(page);
    }

    @ApiOperation("发布一篇新的文章")
    @PutMapping("/")
    @UserLoginToken
    public R<Boolean> postArticle(@RequestBody AcceptArticleModel acceptArticleModel) {
        Article article = Article.builder()
                .content(acceptArticleModel.getContent())
                .label(acceptArticleModel.getTagID())
                .classification(acceptArticleModel.getCategoryId())
                .title(acceptArticleModel.getTitle())
                .releaseTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .status(acceptArticleModel.getStatus())
                .build();

        return R.ok(articleService.save(article));
    }

    @ApiOperation("删除文章")
    @PostMapping("/delete")
    @UserLoginToken
    public R<Boolean> postArticle(@RequestBody List<Integer> removeList) {
        return R.ok(articleService.removeByIds(removeList));
    }


    @ApiOperation("获取文章的所有信息")
    @GetMapping("/id/{aid}")
    @UserLoginToken
    public R<Article> getAllInformationAboutTheArticle(@PathVariable int aid) {
        return R.ok(articleService.getById(aid));
    }


    @ApiOperation("更新文章")
    @PostMapping("/update")
    @UserLoginToken
    public R<Boolean> updateArticle(@RequestBody Article article) {
        article.setUpdateTime(LocalDateTime.now());
        return R.ok(articleService.updateById(article));
    }


    @ApiOperation("改变文章的状态")
    @PostMapping("/change_state/{aid}")
    @UserLoginToken
    public R<Boolean> changeState(@PathVariable int aid) {
        Article byId = articleService.getById(aid);
        byId.setStatus(1 - byId.getStatus());
        return R.ok(articleService.updateById(byId));
    }


    @ApiOperation("丢到废纸篓(单个或批量通用)")
    @PostMapping("/throw_it_in_the_trash")
    @UserLoginToken
    public R<String> throwItInTheTrash(@RequestBody List<Integer> integers) {
        integers.forEach(o -> {
            Article byId = articleService.getById(o);
            byId.setStatus(0);
            articleService.updateById(byId);
        });
        return R.ok("操作成功");
    }

    @ApiOperation("移出废纸篓(单个或批量通用)")
    @PostMapping("/remove_the_trash")
    @UserLoginToken
    public R<String> removeTheTrash(@RequestBody List<Integer> integers) {
        integers.forEach(o -> {
            Article byId = articleService.getById(o);
            byId.setStatus(1);
            articleService.updateById(byId);
        });
        return R.ok("操作成功");
    }


    @ApiOperation("获取废纸篓的文章")
    @PostMapping("/waste_basket/{size}")
    @UserLoginToken
    public R<Page<Article>> getTrash(@PathVariable int size) {
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 0);
        Page<Article> page = articleService.page(new Page<>(size, 10, true), queryWrapper);
        Collection<Article> records = page.getRecords();
        records.forEach(o -> {
            String content = ToolUtil.removeMarkdown(o.getContent());
            if (content.length() > 50) {
                content = content.substring(0, 49) + "...";
            }
            o.setContent(content);
        });
        return R.ok(page);
    }

}